﻿/* Write a program to convert decimal numbers to their binary representation. */
// !!! It will be too easily just to write: string binary = Convert.ToString(decimalNumber, 2);

using System;
using System.Collections.Generic;

class DecimalToBinary
{
    static void Main()
    {
        int decimalNumber = 0;
        bool correct = false;
        do
        {
            try
            {
                Console.Write("Please enter a decimal number: ");
                decimalNumber = int.Parse(Console.ReadLine());
                correct = true;
            }
            catch (FormatException)
            {

                Console.WriteLine("It is not a decimal number.");
            }
            catch (OverflowException)
            {
                Console.WriteLine("Please enter number in the range of {0} to {1}", int.MinValue, int.MaxValue);
            }
        } while (!correct);

        List<int> result = new List<int>();
        int bitCounter = 0;

        while (decimalNumber != 0 && bitCounter != 32)
        {
            bitCounter++;
            var bit = 1 & decimalNumber;
            result.Add(bit);
            decimalNumber = decimalNumber >> 1;
        }

        Console.WriteLine("The binary number is: ");
        for (int i = result.Count-1; i >= 0; i--)
        {
            Console.Write(result[i]);
        }

        Console.WriteLine();
        /*
        bool reverse = false;

        if (decimalNumber < 0)
        {
            decimalNumber = decimalNumber + (decimalNumber * 2);
            reverse = true;
        }

        while (decimalNumber != 0)
        {
            int bit = decimalNumber % 2;
            if (reverse)
            {
                if (bit == 1)
                {
                    bit = 0;
                }
                else
                {
                    bit = 1;
                }
            }
            result.Add(bit);
            decimalNumber /= 2;
        }

        if (reverse)
        {
            
        }

        Console.WriteLine("The binary number is: ");
        for (int i = result.Count-1; i >= 0; i--)
        {
            Console.Write(result[i]);
        }
        Console.WriteLine();
        */
    }
}
